package CutLoad.components
{
	import CutLoad.Base;
	import CutLoad.cookies.Painter;
	
	import flash.display.GradientType;
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.events.MouseEvent;
	import flash.geom.Rectangle;
	

	/**
	 * 绘图面板工作区 
	 * @author smallerbird mail:smallerbird@gmail.com  site:www.mkigm.com QQ群:49983196
	 * 
	 */	
	public class CDrawingPadDisplay extends Base
	{
		private var bak:Base=null;
		private var  bak_color:Number,bak_alpha:Number
		private var pad:Sprite;
		//当前的状态   0没有做什么,1正在画
		private var states:Number=0;
		//是否可以画
		private var isCanDo:Boolean;
		//
		private var line_thickness:Number,line_color:uint,line_alpha:Number,line_pixelHinting:Boolean,line_scaleMode:String,line_caps:String,line_joints:String,line_miterLimit:Number;
		// 构造函数
		public function CDrawingPadDisplay(w:Number,h:Number,bakColor:uint)
		{
			isCanDo=true;
			lineStyle(1,0,1)
			pad=new Sprite();
			this.addChild(pad);
			bak_color=bakColor;
			bak_alpha=1;
			createBak(w,h);
			this.addEventListener(MouseEvent.MOUSE_DOWN, spriteMouseDown);
			this.addEventListener(MouseEvent.MOUSE_UP, spriteMouseUp);
		}
		public function setBakColor(color:Number,alpha:Number):void{
			bak_color=color;
			bak_alpha=alpha;
			//trace(this.getShowWidth()+","+this.getShowHeight());
			createBak(this.getShowWidth(),this.getShowHeight());
		}
		/**
		 * 获得背景 
		 * @return 
		 * 
		 */		
		public function getBak():Base{
			return bak;
		}
		/**
		 * 获得面板 
		 * @return 
		 * 
		 */		
		public function getPad():Sprite{
			return pad;
		}
		protected function createBak(w:Number,h:Number):void{
			if(bak!=null){
				bak.killMe();
			}
			bak=Base.getBase(Painter.getRect(w,h,bak_color,bak_alpha));
			bak.show(this);
			this.swapChildren(pad,bak);
		}
		override public function setWH(w:Number=0, h:Number=0):void{
			if(w==0) w=this.getShowWidth();
			if(h==0) h=this.getShowHeight();
			createBak(w,h);
		}
		public function stopDraw():void{
			states=0;
			this.removeEventListener(MouseEvent.MOUSE_MOVE, spriteMouseMove);
			this.removeEventListener(Event.ENTER_FRAME,enter_frame);
		}
		/**
		 * 可以使用 
		 * 
		 */		
		public function start():void{
			isCanDo=true;
		}
		/**
		 * 不可以使用 
		 * 
		 */		
		public function stop():void{
			isCanDo=false;
		}
		/**
		 * 清除所有页板里的内容 
		 * 
		 */		
		public function clear():void{
			this.pad.graphics.clear();
		}
		/**
		 * 请参看： graphics.lineStyle说明
		 * @param thickness
		 * @param color
		 * @param alpha
		 * @param pixelHinting
		 * @param scaleMode
		 * @param caps
		 * @param joints
		 * @param miterLimit
		 * 
		 */		
		public function lineStyle (thickness:Number, color:uint = 0, alpha:Number = 1.0, pixelHinting:Boolean = false, scaleMode:String = "normal", caps:String = null, joints:String = null, miterLimit:Number = 3):void {
			line_thickness=thickness
			line_color=color
			line_alpha=alpha
			line_pixelHinting=pixelHinting
			line_scaleMode=scaleMode
			line_caps=caps
			line_joints=joints
			line_miterLimit=miterLimit;
		}
		/**
		 * 设置线粗 
		 * @param num
		 * @return 
		 * 
		 */	
		public function setline_thickness(num:Number):void{
			line_thickness=num;
		}
		/**
		 * 设置线色 
		 * @param num
		 * @return 
		 * 
		 */		
		public function setline_color(num:uint):void{
			line_color=num;
		}
		/**
		 * 设置线的透明度 
		 * @param num
		 * @return 
		 * 
		 */		
		public function setline_alpha(num:Number):void{
			line_alpha=num;	
		}
		private function enter_frame(e:Event):void{
			if(states==1){
				//如果正在画，如果过来边界，就取消
				var rectTem:Rectangle=new Rectangle(0,0,bak.getShowWidth(),bak.getShowHeight());
				if(!rectTem.contains(this.mouseX,this.mouseY)){
					stopDraw();
				}
			}
		}
		// 鼠标按下事件
		private function spriteMouseDown(event:MouseEvent):void
		{
			if(!isCanDo) return;
			states=1;
			pad.graphics.lineStyle(line_thickness,line_color,line_alpha,line_pixelHinting,line_scaleMode,line_caps,line_joints,line_miterLimit);
			//pad.graphics.lineGradientStyle(GradientType.LINEAR,[0x000000,line_color],[1,50],[100,100]);
			pad.graphics.moveTo(mouseX, mouseY);
			this.addEventListener(MouseEvent.MOUSE_MOVE, spriteMouseMove);
			this.pad.addEventListener(Event.ENTER_FRAME,enter_frame);
		}
		//
		// 鼠标离开事件
		private function spriteMouseUp(event:MouseEvent):void
		{
			stopDraw();
		}
		
		// 移动事件
		private function  spriteMouseMove(event:MouseEvent):void
		{
			this.pad.graphics.lineTo(mouseX, mouseY);
		}
	}
}